using System;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.WebControls;

namespace AX.WCLib
{
	[DefaultProperty("Text"), 
	    ToolboxData("<{0}:OutLookBar runat=server></{0}:OutLookBar>")]
	public class OutLookBar : WebControl
	{
		private string _Target = "main";
		private int _ImgHeight = 80;
		private int _ImgWidth  = 80;
		private int iGrpIndex  = 0;
		private int _GrpIndex  = -1;
		private StringBuilder objStr;
		
		private string GroupTxt = "<div class=\"xpOLBarContainer\" downbutton=\"DownButton\" upButton=\"UpButton\">" +
								  "<div class=\"xpOLBarContainerButton\" id=\"{0}\"><span class=\"xpOLBarContainerButtonText\">{1}</span></div>" +
								  "<div class=\"xpOLBarButtonContainer\">" +
								  "<div class=\"xpOLBarButtonSpacer\"></div>";

		private string ItemTxt1 = "<tr><td class=\"flyoutMenuItem\" title=\"{4}\" onclick=\"btnClick('{0}')\" noWrap>" +
								  "<img height=\"{2}\" width=\"{3}\" src=\"{1}\" border=\"0\">{4}</td></tr>\r\n";

		private string ItemTxt3 = "<tr><td class=\"flyoutMenuItem\" title=\"{2}\" onclick=\"btnClick('{0}')\" noWrap>" +
								  "<img src=\"{1}\" border=\"0\">{2}</td></tr>\r\n";

		public OutLookBar()
		{
			objStr  = new StringBuilder("");
		}
	
		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string Target 
		{
			get
			{
				return _Target;
			}
			set
			{
				_Target = value;
			}
		}

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue(60)] 
		public int ImgHeight
		{
			get
			{
				return _ImgHeight;
			}

			set
			{
				_ImgHeight = value;
			}
		}

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue(60)] 
		public int ImgWidth
		{
			get
			{
				return _ImgWidth;
			}

			set
			{
				_ImgWidth = value;
			}
		}

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue(0)] 
		public int SelectedGroup
		{
			get
			{
				return iGrpIndex;
			}

			set
			{
				iGrpIndex = value;
			}
		}

		public void AddGroup(string GroupName)
		{
			string strGrpIndex = "Grp" + (++_GrpIndex).ToString();
			objStr.AppendFormat(GroupTxt,strGrpIndex,GroupName);
		}

		public void AddItem(string Url,string ImgSrc,string ItemName)
		{
			objStr.AppendFormat(ItemTxt3,Url.Replace("\\","/"),ImgSrc,ItemName);
		}

		public void AddItem(string Url,int ImgH,int ImgW,string ImgSrc,string ItemName)
		{
			objStr.AppendFormat(ItemTxt1,Url.Replace("\\","/"),ImgSrc,ImgH,ImgW,ItemName);
		}

		public void AddTblStart()
		{
			objStr.Append("<table class=\"flyoutMenu\" cellSpacing=\"0\" cellPadding=\"2\" width=\"100%\" border=\"0\">\r\n");
		}

		public void AddTblEnd()
		{
			objStr.Append("</table></div></div>");
		}

		protected override void Render(HtmlTextWriter output)
		{
			if ( _GrpIndex > -1 )
			{
				output.WriteLine("<a id=\"xpMenuItemsLink\"></a>");
				output.WriteLine("<div class=\"xpOLBar\" id=\"xpOtBar1\" height=\"600\" width=\"100%\" borderStyle=\"2\">");

				output.WriteLine(objStr.ToString());

				output.WriteLine("<div class=\"xpOLBarScrollButton\" id=\"UpButton\">5</div>");
				output.WriteLine("<div class=\"xpOLBarScrollButton\" id=\"DownButton\">6</div>");
				output.WriteLine("</div>");

				output.WriteLine("<script language=\"javascript\">");
				output.WriteLine("function InitOTBar(){");
				output.WriteLine("g_objOtBar=xpOtBar1;");
				output.WriteLine("g_objOtBar.preSlideContainers(Grp"+SelectedGroup.ToString()+");");
				output.WriteLine("ResizeOTBAR();}");
				output.WriteLine("var g_objOtBar;");
				output.WriteLine("window.onload=InitOTBar;");
				output.WriteLine("window.onresize=ResizeOTBAR;");
				output.WriteLine("</script>");
			}
		}
	}
}
